import * as echarts from '../../ec-canvas/echarts';

const app = getApp();
var XList = [];
var YList = [];
let chart = null;//拿出来作为全局变量
var option = {    //拿出来作为全局变量
  title: {
    text: '主标题',
    subtext: '副标题',
    x: 'center',
    align: 'right'
  },
  toolbox: {
    feature: {
      restore: {},
      //magicType: { type: ['line', 'bar'] },
    }
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
      type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
    }
  },
  grid: {
    left: 40,
    bottom: 75,
    show: true,
  },
  legend: {
    data: ['x'],
    x: 'left'
  },
  dataZoom: [{
    type: 'inside',
    start: 0,
    end: 100
  }, {
    handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
    handleSize: '90%',
    handleStyle: {
      color: '#fff',
      shadowBlur: 3,
      shadowColor: 'rgba(0, 0, 0, 0.6)',
      shadowOffsetX: 2,
      shadowOffsetY: 2
    }
  }],
  xAxis: [
    {
      type: 'category',
      boundaryGap: false,
      axisLine: { onZero: false },
      data: YList
    }
  ],
  yAxis: [
    {
      name: '单位',
      type: 'value',

    }
  ],
  series: [{
    name: 'A',
    type: 'line',
    smooth: true,

    data: XList
  }, {
    name: 'B',
    type: 'line',
    smooth: true,
    //   data: [12, 50, 51, 35, 70, 30, 20]
  }, {
    name: 'C',
    type: 'line',
    smooth: true,
    data: [10, 30, 31, 50, 40, 20, 10]
  }]
};

function initChart(canvas, width, height) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  var option1 = option;
  chart.setOption(option1);
  return chart;
}

Page({
  data: {
    ec: {
      onInit: initChart
    }
  },

  onLoad: function (options) {
    //页面一加载就发送请求
    wx.request({
      // url: 'https://www.hyhthjk.com.cn/note.php/wx/Index4/index4',
      url: 'https://www.hyhthjk.com.cn/note.php/js/Index2/index',
      //回调函数成功了的话执行
      success: (res) => {

        console.log(res.data.age);


        for (var i in res.data.age) {
          // for (var i = 0; i < res.length; i++) {
          XList.push(res.data.age[i]);
          // YList.push("25,48,39");
          console.log(res.data.age);
        }
        for (var i in res.data.name) {
          // for (var i = 0; i < res.length; i++) {

          YList.push(res.data.name[i]);
          console.log(res.data.name);

        }


        console.log(XList);
        setTimeout(function () {
          console.log(chart);
          chart.setOption(option);  //赋值后再设置一次option
        }, 10);
        XList = [];
        YList = [];
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
});
